#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ncurses.h> 
#include "PosixTestClient.h"

const unsigned MAX_ATTEMPTS = 50;
const unsigned SLEEP_TIME = 10;

static PosixTestClient client;

int main(int argc, char** argv)
{
	const char* host = argc > 1 ? argv[1] : "";
	unsigned int port = 7496;
	int clientId = 0;
	
  cbreak();
  char c;
   
	int i = 0;
	initscr();
  cbreak();	
	timeout(0);
	printw ("Hit enter to connect\n");
	time_t s = time(NULL);
  
  for (;;) {
		if (time(NULL) - s > 0) {
			s = time(NULL);
			printw ("%d\n", i);
				i++;
			}

		c = getch();
		if (c == '\n') {
			printw ("connecting...\n");
			client.connect (host,port, clientId);
			if (client.isConnected())
				printw ("Connection Successful.\n");
			else 
				printw ("Connection Failed.\n");
    }
		else if (c == 'q')
			break;
  }
    
	endwin();
}
    /*
	for (;;) {
		++attempt;
		printf( "Attempt %u of %u\n", attempt, MAX_ATTEMPTS);

		PosixTestClient client;

		client.connect( host, port, clientId);

		while( client.isConnected()) {
			client.processMessages();
		}

		if( attempt >= MAX_ATTEMPTS) {
			break;
		}

		printf( "Sleeping %u seconds before next attempt\n", SLEEP_TIME);
		sleep( SLEEP_TIME);
	}
*/

